package com.voxel.sense.infomsg.widget.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.com.common.utils.L;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.ConditionBean;
import com.voxel.sense.infomsg.interfaces.listener.onClickSubmitListener;

import java.util.ArrayList;
import java.util.List;

import cyy.example.com.facotry.Factory;

/**
 * Created by chenyiyao on 2017/11/3.
 */

public class FilterRecyclerAdapter extends RecyclerAdapter<ConditionBean> {

    private onClickSubmitListener submitListener;
    private final int TYPE_1 = R.layout.item_select_listview_head;// 头布局
    private final int TYPE_2 = R.layout.item_list_drop_down; //item布局
    private boolean mHeadBtnVisible = true; //按钮是否可见
    private boolean mIsSingleSelect = false; //是否单选
    private int current_position = -1;//单选时，用于记录选中项
    private String mDataType = "DATA_TYPE"; //该适配器的类型
    private String defHeadText = "请选择";  //头布局文本


    public FilterRecyclerAdapter(AdapterListener listener, List<ConditionBean> list, String data_type) {
        super(list, listener);
        this.mDataType = data_type;
    }

    public FilterRecyclerAdapter(AdapterListener listener, String data_type) {
        super( listener);
        this.mDataType = data_type;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_1;
        } else {
            return getItemViewType(position, mDataList.get(position - 1));
        }
    }

    @Override
    protected int getItemViewType(int position, ConditionBean conditionBean) {
        if (position == 0) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }
    }

    @Override
    public int getItemCount() {
        return mDataList.size() + 1;
    }

    @Override
    protected ViewHolder onCreateViewHolder(View root, int viewType) {
        if (viewType == TYPE_1) {
            return new HeadViewHolder(root);
        } else {
            return new ItemViewHolder(root);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_2) {
            L.d("TYPE_2" + position);
            fillValue(position - 1, holder);
        } else {
            HeadViewHolder headHolder = (HeadViewHolder) holder;
            L.d("TYPE_1" + position);
            headHolder.mTvHead.setText(defHeadText);
            headHolder.mHeadBtn.setVisibility(mHeadBtnVisible ? View.VISIBLE : View.GONE);
            onClickSubmit(headHolder);
        }
    }

    //获取选中的数据
    public List getSelectListData() {
        ArrayList list = new ArrayList();
        for (ConditionBean bean : mDataList) {
            if (bean.isSelect()) {
                list.add(bean);
            }
        }
        return list;
    }

    //设置按钮是否可见
    public void setSubmitBtnVisible(boolean str) {
        mHeadBtnVisible = str;
        notifyItemChanged(0);
    }

    //设置描述文本
    public void setHeadText(String str) {
        defHeadText = str;
        notifyItemChanged(0);
    }    //设置描述文本

    public void setSingleSelect(boolean isSingleSelect) {
        this.mIsSingleSelect = isSingleSelect;
    }


    //初始化控件
    private void fillValue(final int position, final RecyclerView.ViewHolder viewHolder) {
        final ItemViewHolder holder = (ItemViewHolder) viewHolder;
        final ConditionBean bean = mDataList.get(position);
        holder.mText.setText(bean.getDesc());
        final Boolean aBoolean = bean.isSelect();
        refreshIcon(holder, aBoolean);
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击按钮，改变状态
                boolean select = !bean.isSelect();
                if (!mIsSingleSelect) {//多选状态
                    refreshIcon(holder, select);
                    mDataList.get(position).setSelect(select);
                } else {//单选状态
                    if (current_position == -1 || (current_position == position)) {
                        //选中自身或还未有选中的数据，这是可以立即刷新数据
                        refreshIcon(holder, select);
                        mDataList.get(position).setSelect(select);


                    } else {
                        //已选中其他选项时，需其他选项还原状态
                        mDataList.get(current_position).setSelect(false);
                        mDataList.get(position).setSelect(true);
                        notifyDataSetChanged();
                    }
                    current_position=position;
                }
                if (mListener != null) {
                    mListener.onItemClick(holder, bean);
                }
            }
        });
    }

    private void refreshIcon(ItemViewHolder holder, Boolean aBoolean) {
        if (aBoolean) {//切换状态
            holder.mText.setTextColor(Factory.app().getResources().getColor(R.color.drop_down_selected));
            holder.mText.setCompoundDrawablesWithIntrinsicBounds(Factory.app().getResources().
                    getDrawable(R.mipmap.icon_select), null, null, null);
        } else {//未选状态
            holder.mText.setTextColor(Factory.app().getResources().getColor(R.color.drop_down_unselected));
            holder.mText.setCompoundDrawablesWithIntrinsicBounds(Factory.app().getResources().
                    getDrawable(R.mipmap.select_user_unclick), null, null, null);
        }
    }

    //设置提交按钮监听
    public void setSubmitListener(onClickSubmitListener listener) {
        this.submitListener = listener;
    }

    //btn按钮监听回调
    private void onClickSubmit(HeadViewHolder viewHolderHead) {
        viewHolderHead.mHeadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (submitListener != null) {
                    submitListener.onSubmit(getSelectListData(), mDataType);
                }
            }
        });
    }


    static class ItemViewHolder extends ViewHolder<ConditionBean> {

        TextView mText;
        View root;

        public ItemViewHolder(View itemView) {
            super(itemView);
            root = itemView;
            this.mText = (TextView) itemView.findViewById(R.id.text);
        }

        @Override
        protected void onBind(ConditionBean conditionBean) {
            mText.setText(conditionBean.getDesc());
        }
    }

    static class HeadViewHolder extends ViewHolder<ConditionBean> {
        View root;
        TextView mTvHead;
        Button mHeadBtn;

        public HeadViewHolder(View view) {
            super(view);
            root = view;
            mTvHead = (TextView) view.findViewById(R.id.tv_head);
            mHeadBtn = (Button) view.findViewById(R.id.btn_head);
        }

        @Override
        protected void onBind(ConditionBean conditionBean) {

        }
    }
}
